/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.afaker.rss.common;

import com.afaker.rss.context.ResourceContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author bruce
 */
public class GeneralUtils {

    public static String getLocalChannelName(String title, boolean isCacheFile, boolean full) {
        String name = "";
        if (full) {
            if (isCacheFile) {
                name += ResourceContext.getResourceContext().getProperty("cache.dir");
            } else {
                name += ResourceContext.getResourceContext().getProperty("archive.dir");
            }
        }
        name += title.replaceAll("/", "-") + ".xml";
        return name;
    }

    public static String getHelpHtml() {
        return 
                "    <h1>How to use RSS Client?</h1>" +
                "    <p>" +
                "      <ul>" +
                "        <li>" +
                "          You can paste the feed url directly to the 'FeedURL' field for a preview." +
                "        </li>" +
                "        <li>" +
                "          In the left bar,<img src='../image/folder_feed.png'/> icon stands for a feeds set that categorized together.The feeds set have a common category." +
                "        </li>" +
                "        <li>" +
                "          And still in the left bar,<img src='../image/feed-icon.gif'/> icon stands for a single feed." +
                "        </li>" +
                "        <li>" +
                "          You can right click to add a category or a channel." +
                "        </li>" +
                "        <li>" +
                "          On the toolbar there several buttons.From the icon you can guess what the button's function is intuitively." +
                "          <br>" +
                "            If you don't what it means you can put the mouse on it,then there will be a tip." +
                "        </li>" +
                "        <li>" +
                "          In the 'Help' menu,there is a menu:'Check for update',clicking it will auto-download the lastest core library file for a update." +
                "        </li>" +
                "      </ul>" +
                "    </p>" +
                "    <p>Note:" +
                "      <br>" +
                "      Generally speaking,feed and channel are the same thing:rss,atom resource." +
                "    </p>" +
                "<p>Among the lucky, you are the chosen one.<br/>" +
                "  -- from <a href='mail-to:toozoofoo@gmail.com'>bruce teng</a>" +
                "    </p>";

    }

    public static void copyFile(String src, String dest) {
        File srcFile = new File(src);
        File destFile = new File(dest);
        FileInputStream reader = null;
        FileOutputStream writer = null;
        try {
            writer = new FileOutputStream(destFile);
        } catch (IOException ex) {
            Logger.getLogger(GeneralUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            reader = new FileInputStream(srcFile);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GeneralUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (srcFile.exists()) {
            int b = -1;
            try {
                while ((b = reader.read()) != -1) {
                    writer.write(b);
                }
            } catch (IOException ex) {
                Logger.getLogger(GeneralUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                writer.flush();
            } catch (IOException ex) {
                Logger.getLogger(GeneralUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(GeneralUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                writer.close();
            } catch (IOException ex) {
                Logger.getLogger(GeneralUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
